package com.vonchange.water.parser.worker;

import com.vonchange.water.domain.main.LineModel;
import com.vonchange.water.domain.main.NodeModel;
import com.vonchange.water.parser.antlr.MermaidFlowBaseVisitor;
import com.vonchange.water.parser.antlr.MermaidFlowParser;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
public class FlowRowVisitor extends MermaidFlowBaseVisitor<String> {

    private Map<String, NodeModel> nodeMap = new HashMap<>();
    private List<LineModel> lineList = new ArrayList<>();
    private String currentLineName=null;
    private List<NodeModel> currentNodes =  new ArrayList<>();
    private String firstNodeId = null;

    public Map<String, NodeModel> getNodeMap() {
        return nodeMap;
    }

    public String getFirstNodeId() {
        return firstNodeId;
    }

    public List<LineModel> getLineList() {
        return lineList;
    }

    @Override
    public String visitNodes(MermaidFlowParser.NodesContext ctx) {
        List<MermaidFlowParser.NodeContext> nodes= ctx.node();
        List<NodeModel> nodeModelList = new ArrayList<>();
        NodeModel nodeModel;
        for (MermaidFlowParser.NodeContext node : nodes) {
            nodeModel = new NodeModel();
            nodeModel.setId(node.idDef().id().getText());
            if(null==firstNodeId){
                firstNodeId=nodeModel.getId();
            }
            if(null!=node.nameDef()){
                nodeModel.setName(getName(node.nameDef()));
                MermaidFlowParser.NameDefContext nameDefContext = node.nameDef();
                if("{".equals(nameDefContext.LEFT().getText())&&"}".equals(nameDefContext.RIGT().getText())){
                    nodeModel.setIsGateway(true);
                }
            }
            if(!currentNodes.isEmpty()){
                List<LineModel> lineModelList = new ArrayList<>();
                LineModel lineModel;
                for (NodeModel currentNode : currentNodes) {
                    lineModel = new LineModel();
                    lineModel.setFromId(currentNode.getId());
                    lineModel.setToId(nodeModel.getId());
                    lineList.add(lineModel);
                    lineModelList.add(lineModel);
                }
                nodeModel.setInLines(lineModelList);
            }
            nodeMap.put(nodeModel.getId(),nodeModel);
            nodeModelList.add(nodeModel);
        }
        currentNodes=nodeModelList;
        return null;
    }
    @Override
    public String visitArrow(MermaidFlowParser.ArrowContext ctx) {
        if(null==ctx.name()){
            currentLineName=null;
            return null;
        }
        currentLineName=null;
        MermaidFlowParser.NameContext nameContext = ctx.name();
        String name=null==nameContext.NAME()?null:nameContext.NAME().getText();
        currentLineName=name;
        return name;
    }
    private String getName(MermaidFlowParser.NameDefContext nameDefContext){
        if(null!=nameDefContext.name().NAME()){
            return nameDefContext.name().NAME().getText();
        }
        if(null!=nameDefContext.name().TEXT()){
            String text = nameDefContext.name().TEXT().getText();
            return text.substring(1,text.length()-1);
        }
        return null;
    }


}